package aria.my12306.aria.my12306.my;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import aria.my12306.R;

public class MyContactEditActivity extends AppCompatActivity {
    private ListView lv;
    List<Map<String,Object>> data=null;
    private Button saveButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_contact_edit);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.back_25);
        actionBar.setDisplayShowHomeEnabled(false);

        lv=(ListView)findViewById(R.id.contactEditList);
        saveButton=(Button)findViewById(R.id.saveEditButton);

        Intent i=getIntent();
        Map<String,Object> contact =(Map<String,Object>)i.getSerializableExtra("row");

        data=new ArrayList<Map<String,Object>>();
        Map<String,Object> row1=new HashMap<>();
        String name = (String) contact.get("name");
        row1.put("key1","姓名");
        row1.put("key2",name.split("\\(")[0]);
        row1.put("key3",R.drawable.forward);
        data.add(row1);

        Map<String,Object> row2=new HashMap<>();
        String idCard = (String) contact.get("idCard");
        row2.put("key1","证件类型");
        row2.put("key2",idCard.split(":")[0]);
        row2.put("key3",null);
        data.add(row2);

        Map<String,Object> row3=new HashMap<>();
        row3.put("key1","证件号码");
        row3.put("key2",idCard.split(":")[1]);
        row3.put("key3",null);
        data.add(row3);

        Map<String,Object> row4=new HashMap<>();

        row4.put("key1","乘客类型");
        row4.put("key2",name.split("\\(")[1].split("\\)")[0]);
        row4.put("key3",R.drawable.forward);
        data.add(row4);


        Map<String,Object> row5=new HashMap<>();
        final String tel = (String) contact.get("tel");
        row5.put("key1","电话");
        row5.put("key2",tel.split(":")[1]);
        row5.put("key3",R.drawable.forward);
        data.add(row5);

        final SimpleAdapter adapter = new SimpleAdapter(MyContactEditActivity.this,data,
                R.layout.item_my_contact_edit,
                new String[]{"key1","key2","key3"},new int[]{
                    R.id.tvContactEditKey,R.id.tvContaantEditValue,R.id.imgContactEditFlg
        });
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                switch (position){
                    case 0:
                        final EditText edtName = new EditText(
                                MyContactEditActivity.this
                        );
                        edtName.setText((String)(data.get(position).get("key2")));
                        edtName.selectAll();

                        new AlertDialog.Builder(MyContactEditActivity.this)
                                .setIcon(R.drawable.right3_25).setTitle("请输入姓名")
                                .setView(edtName)
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        String name=edtName.getText().toString();
                                        if(TextUtils.isEmpty(name)){
                                            setClosable(dialog,false);


                                            edtName.setError("请输入姓名");
                                            edtName.requestFocus();
                                        }else {
                                            setClosable(dialog,true);
                                            data.get(position).put("key2",edtName.getText().toString());
                                            adapter.notifyDataSetChanged();
                                        }
                                    }
                                }).setNegativeButton("取消",null).show();
                        break;
                    case 3:
                        final String[] types= new String[]{"成人","儿童","学生","其他"};
                        String key2=(String)(data.get(position).get("key2"));
                        int idx=0;
                            for(int i=0;i<types.length;i++){
                                if(types[i].equals(key2)){
                                    idx=1;
                                    break;
                                }
                            }
                        new AlertDialog.Builder(MyContactEditActivity.this).setIcon(R.drawable.right3_25)
                                .setTitle("请选择乘客类型")
                                .setSingleChoiceItems(types, idx, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        data.get(position).put("key2",types[which]);
                                        adapter.notifyDataSetChanged();
                                        dialog.dismiss();
                                    }
                                }).setNegativeButton("取消",null).show();
                    case 4:
                        break;
                }
            }
        });
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    private void setClosable(DialogInterface dialog,boolean b){
        Field field;
        try {
            field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
            field.setAccessible(true);
            field.set(dialog,b);
        }catch (NoSuchFieldException e){
            e.printStackTrace();
        }catch (IllegalAccessException e){
            e.printStackTrace();
        }catch (IllegalArgumentException e){
            e.printStackTrace();
        }

    }
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
